package edu.jmu.transaction;

import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:JdbcTemplateBeans.xml")
public class TransactionInitTest {
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Test
	public void insertIntoAccount() {
		String sql1 = "insert into account values(3,'jack1',1000)";
		String sql2 = "insert into account values(4,'tom1',1000)";
		int flag1;
		int flag2;
		try {
			flag1 = jdbcTemplate.update(sql1);
			flag2 = jdbcTemplate.update(sql2);
			if(flag1+flag2 == 2){
				System.out.println("TransactionTest.insertIntoAccount(),插入两条记录成功！");
			}
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("插入失败");
		}
	}
	
	@Test
	public void displayAllAccount(){
		String sql = "select * from account";
		List<Account> accountList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Account>(Account.class));
		for (Account account : accountList) {
			System.out.println(account);
		}
	}
	


}
